package com.duobaoyu.dby.supply.admin.controller.config;

import com.duobaoyu.dby.channel.adapter.constant.ChannelConstant;
import com.duobaoyu.dby.channel.adapter.enums.ChannelCodeEnum;
import com.duobaoyu.dby.common.core.constant.enums.ProductInfoEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.oss.AliOssService;
import com.duobaoyu.dby.common.oss.AliOssTemplate;
import com.duobaoyu.dby.common.oss.model.UpToken;
import com.duobaoyu.dby.supply.common.biz.vo.config.UploadFileConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * oss配置前端上传文件
 *
 * @author jz
 * @version 1.0
 * @date 2022/8/11 14:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/file")
@Api(value = "ali-oss", tags = "oss配置前端上传文件")
public class AliOssController {

	private final AliOssService aliOssService;
	private final AliOssTemplate aliOssTemplate;

//	/**
//	 * 获取上传token
//	 *
//	 * @return
//	 */
//	@GetMapping("/up-token")
//	@ApiOperation(value = "获取Oss上传配置", notes = "获取Oss上传配置")
//	public R<UploadFileVO> getUpToken(String channelCode) {
//		UploadFileVO uploadFileVO;
//		ChannelCodeEnum channelCodeEnum = ChannelCodeEnum.getChannelCodeEnumByCode(channelCode);
//		if(Objects.nonNull(channelCodeEnum) && StringUtils.isNotBlank(channelCodeEnum.getLogo())) {
//			Map<String, String> uploadToken = aliOssTemplate.getUploadToken(ChannelConstant.LOGO_PATH);
//			uploadFileVO = BeanUtil.fillBeanWithMap(uploadToken, ReflectUtil.newInstanceIfPossible(UploadFileVO.class), true);
//			List<String> splits = StrUtil.split(channelCodeEnum.getLogo(), StringPool.SLASH, Boolean.TRUE, Boolean.TRUE);
//			uploadFileVO.setFileName(splits.get(splits.size() - CommonConstants.ONE));
//			uploadFileVO.setIfOverride(Boolean.FALSE);
//		} else {
//			UpToken upToken = new UpToken();
//			upToken.setProductInfo(ProductInfoEnum.SUPPLY);
//			UploadTokenVO uploadTokenVO = aliOssService.getUpToken(upToken);
//			uploadFileVO = supplyChainAdminConverter.toUploadFile(uploadTokenVO);
//			uploadFileVO.setIfOverride(Boolean.TRUE);
//		}
//		return R.ok(uploadFileVO);
//	}


	/**
	 * 获取上传token
	 *
	 * @return
	 */
	@GetMapping("/token")
	@ApiOperation(value = "获取Oss上传配置", notes = "获取Oss上传配置")
	public R<UploadFileConfigVO> getUpToken(@RequestParam("fileName") String fileName, @RequestParam("channelCode") String channelCode) {
		Map<String, String> uploadToken;
		ChannelCodeEnum channelCodeEnum = ChannelCodeEnum.getChannelCodeEnumByCode(channelCode);
		if(Objects.nonNull(channelCodeEnum) && StringUtils.isNotBlank(channelCodeEnum.getLogo())) {
			uploadToken = aliOssTemplate.getUploadTokenMap(null, ChannelConstant.LOGO_PATH + fileName);
		} else {
			UpToken upToken = new UpToken();
			upToken.setProductInfo(ProductInfoEnum.SUPPLY);
			upToken.setFileName(fileName);
			uploadToken = aliOssService.getUpToken(upToken);
		}
		UploadFileConfigVO vo = new UploadFileConfigVO();
		vo.setHost(uploadToken.get("host"));
		vo.setUrl(uploadToken.get("url"));
		return R.ok(vo);
	}

}
